ServiceActivitySetting = function (editor) {
    this.editor = editor;
    this.setting = this.getCurrentAttributeSetting("service");
};

mxUtils.extend(ServiceActivitySetting, AttributeSetting);

/**
 * 渲染子页面
 */
ServiceActivitySetting.prototype.renderServiceActivitySetting = function () {
    // 基本属性渲染页面
    this.renderBasicAttrOfSubProcessActivity();
    // 子页面 服务配置渲染
    this.renderExtra();
};


/**
 * 渲染自由活动的基本属性设置页面
 */
ServiceActivitySetting.prototype.renderBasicAttrOfSubProcessActivity = function () {
    var basicAttrRegion = $('#ef_region_basicAttr');
    $('#ID', basicAttrRegion).attr('value', this.setting.getAttribute('ID'));
    $('#name', basicAttrRegion)
        .attr('value', this.setting.getAttribute('Name'));
    $('#displayName', basicAttrRegion).attr('value',
        this.setting.getAttribute('DisplayName'));
    $("#description").val(this.setting.getAttribute('Description'));
};

/**
 * 渲染服务配置子页面
 *
 * @return
 */
ServiceActivitySetting.prototype.renderExtra = function () {
    var dom = $(this.setting);
    var block = new Array();
    var grid = $("#ef_grid_extraResult").data("kendoGrid");

    function extraResult(ParamName, ParamDesc, ParamCateGory, ParamValue, ParamType, ParamInOut) {
        this.ParamName = ParamName;
        this.ParamDesc = ParamDesc;
        this.ParamCateGory = ParamCateGory;
        this.ParamValue = ParamValue;
        this.ParamType = ParamType;
        this.ParamInOut = ParamInOut;
    }

    // 加载扩展属性数据
    $(dom).find('ServiceParam').each(function () {
        var row = new extraResult();
        $.each(this.attributes, function (i, attrib) {
            var name = attrib.name;
            var value = attrib.value;
            row[this.name] = this.value;
        });
        block.push(row);
    });
    grid.dataSource.data(block);

    // ServiceID  ServiceType 数据回显
    var extraAttrRegion = $('#serviceInfoSet');
    var serviceIDValue = $(dom).find('ServiceID').text();
    $('#ServiceID', extraAttrRegion).attr('value', serviceIDValue);

    var handleTypes = $('input', extraAttrRegion);
    var async = $(dom).find('Async').text();
    handleTypes.each(function () {
        if ($(this).attr("value") == async)
            $(this).attr("checked", "checked");
    });
};


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~业务处理~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
 * 保存自由流程活动属性设置
 */
ServiceActivitySetting.prototype.updateServiceSetting = function () {
// 子标签页分别保存
    this.updateBasicAttributeOfServiceActivitySetting();
    this.updateServiceConfigureOfServiceActivitySetting();
// 数据保存
    this.updateAttributeSetting();
};

/**
 * 保存自由活动基本属性设置
 */
ServiceActivitySetting.prototype.updateBasicAttributeOfServiceActivitySetting = function () {
    // 获取活动属性配置
    var basicAttrRegion = $('#ef_region_basicAttr');
    var id = $('#ID', basicAttrRegion).attr('value');  // 服务ID
    var name = $("#name").val();
    var displayName = $("#displayName").val();
    var description = $("#description").val();

    // 设置基本属性 保存到cas中 流程保存时会保存到数据库中
    var cas = this.setting;
    cas.setAttribute('ID', id);
    cas.setAttribute('Name', name);
    cas.setAttribute('DisplayName', displayName);
    cas.setAttribute('Description', description);

};

/**
 * 保存自由活动节点子页面-服务配置
 *<serviceID>U4000</serviceID>
 *<serviceType>动态</serviceType>
 *<Extras>
 *     <ServiceParams>
 *                 <ServiceParam  ParamName = "A" ParamDesc="AA" ParamValue="588" ParamType="String" ParamInOut="constants" ParamCateGory="virable" />
 *     </ServiceParams>
 *</Extras>
 * @return
 */
ServiceActivitySetting.prototype.updateServiceConfigureOfServiceActivitySetting = function () {
    // 设置扩展属性
    var doc = this.createXmlDocument();
    var serviceParams = $(this.setting).find('ServiceParams')[0];
    var serviceID = $(this.setting).find('ServiceID')[0];
    var Async = $(this.setting).find('Async')[0];

    var extraAttrRegion = $('#serviceInfoSet');

    if (serviceParams == null) {
        serviceParams = doc.createElement('ServiceParams');
        $(this.setting).find('Extras')[0].appendChild(serviceParams);
    } else
        $(serviceParams).empty();

    // 初始化DOM中的 serviceID 节点
    if (serviceID == null) {
        obj = document.getElementsByName("ServiceID");
        var serviceID = $('#ServiceID').val();
        serviceIDElement = doc.createElement('ServiceID');
        serviceIDElement.textContent = serviceID;
        $(this.setting).find('Extras')[0].appendChild(serviceIDElement);
    } else {
        //更新serviceID的值
        var serviceInfoSetAttrRegion = $('#serviceInfoSet');
        // 获取输入框输入的 ServiceID 的值
        var ServiceIDCurrentValue = $('#ServiceID').val();

        // Extras中使用新节点替换原有的节点
        oldServiceIDElement = $(this.setting).find('ServiceID')[0];
        $(this.setting).find('Extras')[0].removeChild(oldServiceIDElement);
        newServiceIDElement = doc.createElement('ServiceID');
        newServiceIDElement.textContent = ServiceIDCurrentValue;
        $(this.setting).find('Extras')[0].appendChild(newServiceIDElement);
    }

    if (Async == null) {
        var AsyncValue = $("input[name='Async']:checked").val();
        asyncElement = doc.createElement('Async');
        asyncElement.textContent = AsyncValue;
        $(this.setting).find('Extras')[0].appendChild(asyncElement);
    } else {
        var AsyncValue = $("input[name='Async']:checked").val();
        oldAsyncElement = $(this.setting).find('Async')[0];
        $(this.setting).find('Extras')[0].removeChild(oldAsyncElement);
        newAsyncElement = doc.createElement('Async');
        newAsyncElement.textContent = AsyncValue;
        $(this.setting).find('Extras')[0].appendChild(newAsyncElement);
    }

    var block = extraResultGrid.getBlockData();
    var variable = null;
    if (block.getRows().length > 0)
        for (var i = 0; i < block.getRows().length; i++) {
            variable = doc.createElement('ServiceParam');
            variable.setAttribute('ParamName', block.getCell(i, 'ParamName'));
            variable.setAttribute('ParamDesc', block.getCell(i, 'ParamDesc'));
            variable.setAttribute('ParamCateGory', block.getCell(i, 'ParamCateGory'));
            variable.setAttribute('ParamValue', block.getCell(i, 'ParamValue'));
            variable.setAttribute('ParamType', block.getCell(i, 'ParamType'));
            variable.setAttribute('ParamInOut', block.getCell(i, 'ParamInOut'));
            serviceParams.appendChild(variable);
        }
};

ServiceActivitySetting.prototype.verifyPartHasSelected = function (partId, type, partsBlock) {
    var verifyPartHasSelected = false;
    if (partsBlock.getRows().length > 0)
        for (var i = 0; i < partsBlock.getRows().length; i++) {
            if (partsBlock.getCell(i, 'Id') == partId
                && partsBlock.getCell(i, 'Type') == type) {
                verifyPartHasSelected = true;
                break;
            }
        }
    return verifyPartHasSelected;
};